// packageJ/chainStore/chainStoreIndex/chainStoreIndex.js
const app = getApp();
const skinBehavior = require('../../../utils/themeSkin/skinBehavior');
var location = require("../../../mybehaviors/location/location");
const storeGood = require('../mycomponent/storeGood')
const cartPost = require('../mycomponent/cart');
Page({
  behaviors: [skinBehavior, storeGood, cartPost, location],
  /**
   * 页面的初始数据
   */
  data: {
    keyWord: "",
    category: [],
    info: [],
    noticeShow: false,
    storeDetailShow: false,
    showCarList: false,
    get_type: 1,
    store_id: "30",
    lng: "113.238337",
    lat: "23.260773",
    delivery_area: [],
    delivery: [],
    locations: [],
    themeColor: "",
    address_id: "",
    themeBaseColorOpacity: '',
    markers_2: [],
    polygons_2: [{
      points: [],
      fillColor: "#FF88887F",
      strokeColor: "#FFF",
      strokeWidth: 2,
      zIndex: 1
    }],
    manySpecsInfo: [],
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    console.log(wx.getSystemInfoSync().statusBarHeight)
    // get_type=1&lng=113.2732&lat=23.157159&store_id=30
    this.data.options = options;
    this.setData({
      statusBarHeight: wx.getSystemInfoSync().statusBarHeight
    })
    let sysInfo = wx.getSystemInfoSync();
    let menuInfo = wx.getMenuButtonBoundingClientRect();
    let navigationBarHeight = (menuInfo.top - sysInfo.statusBarHeight) * 2 + menuInfo.height;
    console.log(navigationBarHeight)
    if (this.data.options.get_type) {
      this.setData({
        get_type: this.data.options.get_type
      })
    }
    if (this.data.options.store_id) {
      this.setData({
        get_type: this.data.options.get_type
      })
    }
    if (this.data.options.lng) {
      this.setData({
        lng: this.data.options.lng
      })
    }
    if (this.data.options.lat) {
      this.setData({
        lat: this.data.options.lat
      })
    }
    if (this.data.options.store_id) {
      this.setData({
        store_id: this.data.options.store_id
      })
    }
    if (this.data.options.lng) {
      this.setData({
        lng: this.data.options.lng
      })
    }
    if (this.data.options.address_id) {
      this.setData({
        address_id: this.data.options.address_id
      })
    }
    if (this.data.get_type == 4) {
      let myLocation = wx.getStorageSync('address-point');
      if (myLocation) {
        this.setData({
          city: myLocation.city,
          point: myLocation.point
        })
        this.getData()
      } else {
        this._getLocation((res) => {
          this.setData({
            city: res.ad_info.city,
            point: res.ad_info.location
          })
          this.getData();
        }, (err) => {
          this.getData();
        });
      }
    } else {
      this.getData();
    }
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {
    this.setData({
      keyWord: ''
    })
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {
    this.data.observer.disconnect();
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  },
  tapOption(e) {
    let {
      item
    } = e.currentTarget.dataset;
    this.setData({
      manySpecsInfo: item
    })
    console.log(e, item)
  },
  changeStepper(e) {
    let {
      id
    } = e.currentTarget.dataset;
    this.addCart(id, this.data.store_id, '', true)
  },
  updateCart() {
    this.getCart();
  },
  toNoticeUrl(e) {
    let {
      url
    } = e.currentTarget.dataset;
    wx.navigateTo({
      url: url
    })
  },
  storeDetailShowTap() {
    this.setData({
      storeDetailShow: true
    })
  },
  onClosemanySpecs() {
    this.setData({
      manySpecsInfo: []
    })
    this.updateCart();
  },
  onClose() {
    this.setData({
      noticeShow: false,
      storeDetailShow: false
    })
  },
  toNoticeUrl_show() {
    this.setData({
      noticeShow: true
    })
  },
  toSearchGood() {
    wx.navigateTo({
      url: `/packageJ/chain-store/chainStoreSearch/chainStoreSearch?store_id=${this.data.store_id}&keyword=${this.data.keyWord}`
    })
  },
  getTypeChange() {
    wx.redirectTo({
      url: '/packageJ/chain-store/chainStoreSearchStore/chainStoreSearchStore',
    })
  },
  toBack() {
    wx.navigateBack({
      delta: 1,
      fail: function (err) {
        console.log(err)
        wx.reLaunch({
          url: '/packageJ/chain-store/chainStoreSearchStore/chainStoreSearchStore',
        })
      }
    })
  },
  getData() {
    let json = {
      get_type: this.data.get_type,
      store_id: this.data.store_id,
      lat: this.data.lat,
      lng: this.data.lng
    };
    if (this.data.get_type != 1) {
      json.address_id = this.data.address_id;
    }
    if (this.data.get_type === "4") {
      json = {};
      json.get_type = this.data.get_type;
      json.lat = this.data.lat;
      json.lng = this.data.lng;
    }
    let urlStr = app.getNetAddresss("plugin.chain-store.frontend.controller.index.index");
    app._postNetWork({
      url: urlStr,
      data: json,
      success: (resdata) => {
        let res = resdata.data
        console.log(res);
        if (res.result == 1) {
          this.setData({
            info: res.data,
            themeBaseColorOpacity: wx.getStorageSync('yz_basic_info').theme_color + '10',
            store_id: res.data.store_info.store_id
          })
          this.getCart();
          if (this.data.info.store_info.store_delivery.locations) {
            this.setData({
              "markers_2[0].longitude": this.data.info.store_info.store_delivery.locations.longitude,
              "markers_2[0].latitude": this.data.info.store_info.store_delivery.locations.latitude,
              "delivery.longitude": this.data.info.store_info.store_delivery.locations.longitude,
              "delivery.latitude": this.data.info.store_info.store_delivery.locations.latitude,
            });
            if (this.data.info.store_info.store_delivery.delivery_area) {
              let points = [];
              for (let i = 0; i < this.data.info.store_info.store_delivery.delivery_area.length; i++) {
                points.push({
                  longitude: this.data.info.store_info.store_delivery.delivery_area[i].R,
                  latitude: this.data.info.store_info.store_delivery.delivery_area[i].Q
                });
              }
              this.setData({
                "polygons_2[0].points": points,
              });
            }
          }
          if (this.data.info.category && this.data.info.category.length > 0) {
            this.setData({
              category: res.data.category,
              oneInd: 0,
              twoInd: 0
            })
            this.getCateGood();
          }
        } else {
          app.tips(res.msg)
        }
      },
      fail: function (res) {}
    });
  },
})